import Vue from 'vue'
import Router from 'vue-router'
import axios from 'axios'

import Login from '@components/Pages/Login/login'
import Register from '@components/Pages/Login/register'
import Course from '@components/Pages/Course'
import Sign from '@components/Pages/Sign'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      component: Course
    },
    {
      name: 'course',
      path: '/course',
      component: Course
    },
    {
      name: 'sign',
      path: '/sign',
      component: Sign
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/register',
      component: Register
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === 'register') {
    return next()
  }
  if (sessionStorage.getItem('count') === '1') {
    return next()
  }
  axios.get('/others/verify_token').then(res => {
    if (res.data.status === 'fail') {
      sessionStorage.setItem('count', 1)
      next('/login')
    }
  })
})

export default router
